<?php

namespace App\Admin\Controllers;

use App\Models\Address;
use App\Models\Area;
use App\Util\Tool;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\MessageBag;

class ReturnaddressController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '退货地址管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Address());
        $grid->disableExport();//禁止导出
        // 去掉筛选工具
        $grid->tools(function (Grid\Tools $tools) {
            $tools->disableFilterButton();
        });
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
        });
        $grid->actions(function ($actions) {
            // 去掉查看
            $actions->disableView();
            $actions->disableDelete();
        });
        $userid = auth()->user()->id ?? 0;
        $is_work = auth()->user()->is_work;
        $where = [];
        if($is_work>0){ //工作室
            $where [] = ['user_id', '=', $is_work];
            $where [] = ['type', '=', 2];
        }else{
            $where [] = ['user_id', '=', $userid];
            $where [] = ['type', '=', 1];
        }
        $grid->model()->where($where)->orderByDesc('id');//只查看审核过的商品
        $grid->column('id', __('Id'));
        $grid->column('name', __('收货人姓名'));
        $grid->column('phone', __('电话'));
        $grid->column('detail', __('详细地址'));
        $grid->column('order_delivery_addr', __('详细地址'))->display(function () {
            $area = new Area();
            $province_name = $area->where('code', $this->province_id ?? 0)->value('name');
            $city_name = $area->where('code', $this->city_id ?? 0)->value('name');
            $district_name = $area->where('code', $this->district_id ?? 0)->value('name');
            $detail = $this->detail ?? '';
            return $province_name . '->' . $city_name . '->' . $district_name . '->' . $detail;
        });
//        $grid->column('province_id', __('Province id'));
//        $grid->column('city_id', __('City id'));
//        $grid->column('district_id', __('District id'));
//        $grid->column('created_at', __('Created at'));
//        $grid->column('updated_at', __('Updated at'));
//        $grid->column('user_id', __('User id'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Address::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('status', __('Status'));
        $show->field('name', __('Name'));
        $show->field('phone', __('Phone'));
        $show->field('detail', __('Detail'));
        $show->field('province_id', __('Province id'));
        $show->field('city_id', __('City id'));
        $show->field('district_id', __('District id'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('user_id', __('User id'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Address());
        $userid = auth()->user()->id ?? 0;
        $create = $form->isCreating();
        $is_work = auth()->user()->is_work;
        if ($create) {
            if ($is_work > 0) { //工作室
                $has = (new Address())->where([['type', '=', 2], ['user_id', '=', $is_work]])->first();
            } else {
                $has = (new Address())->where([['type', '=', 1], ['user_id', '=', $userid]])->first();
            }
            if ($has) {
                $error = new MessageBag([
                    'title' => '您已存在退货地址,不能再次添加,如需修改,请编辑信息！'
                ]);
                return redirect(url("admin/returnaddress"))->with(compact('error'));
//                admin_toastr('您的退货地址已存在,不能重复添加!',"error",['timeOut' => 2000]);
//                return redirect()->back();
//                return redirect('/admin/returnaddress');
            }
        }
//        $form->number('status', __('Status'));
        $form->text('name', __('收货人姓名'))->rules('required');
        $form->mobile('phone', __('电话'))->rules('required');
        $form->distpicker(['province_id', 'city_id', 'district_id'], '请选择区域')->rules('required');
        $form->text('detail', __('详情地址'))->rules('required');
//        $form->text('province_id', __('Province id'));
        if ($is_work > 0) {
            $form->hidden("type", "类型")->value(2);
            $form->hidden("user_id","用户ID")->value($is_work);
        } else {
            $userid = auth()->user()->id ?? 0;
            $form->hidden("user_id","用户ID")->value($userid);
            $form->hidden("type", "类型")->value(1);
        }

//        $form->text('city_id', __('City id'));
//        $form->text('district_id', __('District id'));
//        $form->number('user_id', __('User id'));
        return $form;
    }
}
